<?php
header("Content-Type: text/html; charset=utf-8");
	$cityName = "哈哈";
	$citycode = fromKeywordToCode($cityName);
    if ($citycode == "")
		{
			$citycodenull = array();
			 $citycodenull[] = array("Title" =>"没有该城市？请重新查询", "Description" =>"", "PicUrl" =>"", "Url" =>"http://www.weather.com.cn");
			 var_dump ($citycodenull);

		} 
		else
		{
    $winddirection = array("0"=>"无持续风向","1"=>"东北风","2"=>"东风","3"=>"东南风","4"=>"南风","5"=>"西南风","6"=>"西风","7"=>"西北风","8"=>"北风","9"=>"旋转风");
    $windpower = array("0"=>"微风","1"=>"3-4级","2"=>"4-5级","3"=>"5-6级","4"=>"6-7级","5"=>"7-8级","6"=>"8-9级","7"=>"9-10级","8"=>"10-11级","9"=>"11-12级");
    $phenomenon = array("00"=>"晴","01"=>"多云","02"=>"阴","03"=>"阵雨","04"=>"雷阵雨","05"=>"雷阵雨伴有冰雹","06"=>"雨夹雪","07"=>"小雨","08"=>"中雨","09"=>"大雨","10"=>"暴雨","11"=>"大暴雨","12"=>"特大暴雨","13"=>"阵雪","14"=>"小雪","15"=>"中雪","16"=>"大雪","17"=>"暴雪","18"=>"雾","19"=>"冻雨","20"=>"沙尘暴","21"=>"小到中雨","22"=>"中到大雨","23"=>"大到暴雨","24"=>"暴雨到大暴雨","25"=>"大暴雨到特大暴雨","26"=>"小到中雪","27"=>"中到大雪","28"=>"大到暴雪","29"=>"浮尘","30"=>"扬沙","31"=>"强沙尘暴","53"=>"霾","99"=>"无");
	$forecast_vInfo = json_decode(getWeatherData($citycode, "forecast_v"), true);  //获取常规参数
	$indexInfo = json_decode(getWeatherData($citycode, "index_v"), true);          //获取指数参数
    $weatherArray = array();                                                       //定义回复数组
	//标题
    $weatherArray[] = array("Title" =>$cityName."天气预报", "Description" =>"", "PicUrl" =>"", "Url" =>"");    
    //3日
    $day3Info = $forecast_vInfo["f"]["f1"];
    $weekArray = array("日","一","二","三","四","五","六");
	for ($i = 0; $i < count($day3Info); $i++) 
		{
			if (date("H") < 18)
				{
					$offset = strtotime("+".$i." day");
					$forecast3dtitle = date("m月d日",$offset).
					" 周".$weekArray[date('w',$offset)]." ".
					$phenomenon[$day3Info[$i]["fa"]].
					(($day3Info[$i]["fa"] != $day3Info[$i]["fb"])?("转".$phenomenon[$day3Info[$i]["fb"]]):"")." ".
					$day3Info[$i]["fc"]."℃/".$day3Info[$i]["fd"]."℃ ".
					$winddirection[$day3Info[$i]["fe"]]." ".$windpower[$day3Info[$i]["fg"]]." ".
					"日出日落：".str_replace("|", "~", $day3Info[$i]["fi"]);
					$picurl = "http://www.dataoffice.cn/weixin/weather/icon/day".$day3Info[$i]["fa"].".jpg";
				}
			else
				{
					$offset = strtotime("+".($i+1)." night");
					$forecast3dtitle = date("m月d日",$offset).
					" 周".$weekArray[date('w',$offset)]." ".
					$phenomenon[$day3Info[$i]["fb"]]." ".
					$day3Info[$i]["fd"]."℃".
					$winddirection[$day3Info[$i]["fe"]]." ".$windpower[$day3Info[$i]["fg"]]." ".
					"日出日落：".str_replace("|", "~", $day3Info[$i]["fi"]);
					$picurl = "http://www.dataoffice.cn/weixin/weather/icon/night".$day3Info[$i]["fb"].".jpg";
				}
			$weatherArray[] = array("Title" =>trim($forecast3dtitle), "Description" =>"", "PicUrl" =>$picurl, "Url" =>"");
		}
	//指数
    $indextitle = "";
	for ($i = 0; $i < count($indexInfo["i"]); $i++)
		{
			$indextitle = $indexInfo["i"][$i]["i2"]."【".$indexInfo["i"][$i]["i4"]."】".$indexInfo["i"][$i]["i5"]."\n";
			$weatherArray[] = array("Title" =>trim($indextitle), "Description" =>"", "PicUrl" =>"", "Url" =>""); 
		}
var_dump( $weatherArray);
		}

function getWeatherData($areaid, $type)
{
    $private_key = '84cb28_SmartWeatherAPI_c23cc01';
	$appid='bd487063600a7556';
	$appid_six=substr($appid,0,6);
	$date=date("YmdHi");
	$public_key="http://open.weather.com.cn/data/?areaid=".$areaid."&type=".$type."&date=".$date."&appid=".$appid; 
	$key = base64_encode(hash_hmac('sha1',$public_key,$private_key,TRUE)); 
	$url="http://open.weather.com.cn/data/?areaid=".$areaid."&type=".$type."&date=".$date."&appid=".$appid_six."&key=".urlencode($key);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$output = curl_exec($ch);
	curl_close($ch);
    return $output;
}
function fromKeywordToCode($keyword)
{
    $mysql_host = "localhost";
    $mysql_user = "root";
    $mysql_password = "password";
    $mysql_database = "weixin"; 
	$mysql_table = "weather";
    $mysql_state = "SELECT * FROM ".$mysql_table." WHERE cityName LIKE '%".$keyword."%' LIMIT 0 , 1";
	$con = mysql_connect($mysql_host, $mysql_user, $mysql_password);
	if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
	mysql_query("SET NAMES UTF8");
	mysql_select_db($mysql_database, $con);
	$result = mysql_query($mysql_state,$con);
    $citycode = ""; 
    while($row = mysql_fetch_array($result))
    {
        $citycode = $row[cityCode]; 
        break;
    }
	mysql_close($con);
	return $citycode;
}
?>